/**
 *  Copyright (C) 2008-2013  Telosys project org. ( http://www.telosys.org/ )
 *
 *  Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *          http://www.gnu.org/licenses/lgpl.html
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.telosys.common.logger;

import java.io.InputStream;
import java.util.Properties;

import org.telosys.util.PropUtil;

/**
 * Trace configuration holder.<br>
 * Keep the "trace" flag by class name.
 * 
 * @author Laurent GUERIN
 *  
 */

public class TraceConfig
{
    private static final String CLASS_NAME = TraceConfig.class.getName() ;

    private static Properties   _flags     = null;  // Flags list (access by class name)

    private static int          _iState    = 0;     // Current State of the configuration

    //-----------------------------------------------------------------------------
    // Init
    //-----------------------------------------------------------------------------
    /**
     * Init from properties
     * @param prop
     */
    public static void init(Properties prop)
    {
        TelosysLogger.info(CLASS_NAME, "init(Properties)");
        _flags = prop;
        _iState++;
        TelosysLogger.info(CLASS_NAME, "init : " + _flags.size() + " classes in the configuration ");
    }

    //-----------------------------------------------------------------------------
    /**
     * Init from properties file name
     * @param sFileName
     */
    public static void init(String sFileName)
    {
        TelosysLogger.info(CLASS_NAME, "init('" + sFileName + "')");
        //_flags = Tool.loadProperties(sFileName);
        _flags = PropUtil.loadFromFileSystem(sFileName); // v 1.2.0
        _iState++;
        TelosysLogger.info(CLASS_NAME, "init : " + _flags.size() + " classes in the configuration ");
    }

    //-----------------------------------------------------------------------------
    /**
     * Init from InputStream
     * @param is
     */
    public static void init(InputStream is)
    {
        TelosysLogger.info(CLASS_NAME, "init(InputStream)");
        //_flags = Tool.loadProperties(is);
        _flags = PropUtil.loadFromInputStream(is); // v 1.2.0
        _iState++;
        TelosysLogger.info(CLASS_NAME, "init : " + _flags.size() + " classes in the configuration ");
    }


    //-----------------------------------------------------------------------------
    /**
     * Return the current state (numeric stamp) 
     * @return
     */
    public static int getCurrentState()
    {
        return _iState;
    }

    //-----------------------------------------------------------------------------
    /**
     * Return the "trace" flag state for the given class
     * @param sClassName
     * @return
     */
    public static boolean flagIsOn(String sClassName)
    {
        return getFlag(sClassName);
    }

    //-----------------------------------------------------------------------------
    /**
     * Return the current "trace" flag for the given class
     * @param sClassName
     * @return
     */
    public static boolean getFlag(String sClassName)
    {
        if (_flags == null)
        {
            return false;
        }
        else
        {
            String sFlag = _flags.getProperty(sClassName, "0"); // False =
            // default value
            if (sFlag != null)
            {
                if (sFlag.startsWith("1"))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
    }

    //-----------------------------------------------------------------------------
    public static void setFlag(Class<?> clazz, boolean flag)
    {
    	setFlag(clazz.getCanonicalName(), flag);
    }
    
    //-----------------------------------------------------------------------------
    /**
     * Set the "trace" flag for the given class 
     * @param sClassName
     * @param bFlag
     */
    public static void setFlag(String sClassName, boolean bFlag)
    {
        if (_flags == null)
        {
            _flags = new Properties();
        }
        if (bFlag == true)
        {
            _flags.setProperty(sClassName, "1");
        }
        else
        {
            _flags.setProperty(sClassName, "0");
        }
        _iState++;
    }

}